

<template>
	<div id="abb"class="dev-body-box" :style="{ width: windowWidth + 'px', height: windowHeight + 'px'}">
		
	</div>
</template>

<script setup lang="ts" name="Follow">


import { useStore } from 'vuex';
import { ref,onMounted} from "vue";
import { readProjectPathInfo } from '../utils';
import build from '../compile/commanders/build';
/* const CoffeeScript = require('coffee-script');
CoffeeScript.register();
console.log(CoffeeScript) */

//========= start 小程序引入模块 ====================//
// import '../assets/mini_program/main.css'
import { Device } from '../mini_program/core/device/device.js'
import { Application } from '../mini_program/core/application';
import { AppManager } from "../mini_program/core/appManager";
//========= end 小程序引入模块 ====================//


// props
/* const props = defineProps({
	//是否往下执行程序
	run: {
		type: Boolean,
		default: false
	},

}); */

//data
//获取当前组件实例，然后通过 proxy 属性访问 $bus
// const { proxy } = getCurrentInstance();
const store=useStore();

const windowWidth = ref('');
const windowHeight = ref('');


onMounted(()=>{
	
	/* proxy.$bus.on('mitt-message', (res) => {
		console.log('on-mitt-message', res)
		if(res.type=='loadNewProject'){
			loadProject();
		}else if(res.type=='recompileProject'){
			loadProject();
		}else if(res.type=='createProjectSuccess'){
			loadProject();
		}
	}); */

	loadProject();

})

//加载项目
const loadProject = async () => {
	//读取本地工程中的项目路径
	const projectInfo = await readProjectPathInfo('project.config.json');
	console.log('projectInfo:', projectInfo);
	if (projectInfo.entryProjectPath) {
		let projectPath = projectInfo.entryProjectPath;

		store.state.projectInfo.path=projectPath

		window.electron.ipcRenderer.send('set-title', projectPath);

		//编译项目
		build({publishPath:projectPath},async ()=>{

			//打开小程序

			//读取小程序项目中的配置信息
			const projectConfigInfo =await readProjectPathInfo(projectPath+"/project.config.json");
			console.log('projectConfigInfo:', projectConfigInfo);
			windowWidth.value = projectConfigInfo.windowWidth;
			windowHeight.value = projectConfigInfo.windowHeight;

			store.state.projectInfo.appId = projectConfigInfo.appid;
			store.state.projectInfo.windowWidth = projectConfigInfo.windowWidth;
			store.state.projectInfo.windowHeight = projectConfigInfo.windowHeight;

			//读取app.js中的配置信息
			const appConfigInfo =await readProjectPathInfo(projectPath+"/app.json");

			store.state.projectInfo.entryPath=appConfigInfo.pages[0];

			const appInfo = {appId:projectConfigInfo.appid,path:appConfigInfo.pages[0]};
			
			const dev = new Device();
			
			const wx = new Application();
			
			dev.open(wx);
	
			AppManager.openApp({
				appId: appInfo.appId,
				path: appInfo.path,
				scene: 1001,
				currProjectPath:projectPath+'/dist',
				windowWidth: projectConfigInfo.windowWidth,
				windowHeight: projectConfigInfo.windowHeight,
			}, wx);

		});
	
	}
}


</script>

<style scoped>
.dev-body-box {
	width: 80%;
	height: 80%;
	margin:10px auto;
	box-shadow:0 0 5px #ddd;
	border-radius: 10px;
	overflow: hidden;
}

</style>